package com.abstralabs.time;

import java.util.Calendar;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class TimeDatabaseHandler {
	
	private SQLiteDatabase database;
	private TimeDatabase timedb;

	  private String[] allColumns = { TimeDatabase.COLUMN_ID,
			  TimeDatabase.COLUMN_ACT, 
			  TimeDatabase.COLUMN_SEC,
			  TimeDatabase.COLUMN_DAY };
	  
	  public TimeDatabaseHandler(Context context) {
		    timedb = new TimeDatabase(context);
		  }
	  
	  public void open() throws SQLException {
	    database = timedb.getWritableDatabase();
	  }

	  public void close() {
	    timedb.close();
	  }

	  public void addEvent(Calendar calnow, String activity) {
	    ContentValues values = new ContentValues();
//	    values.put(TimeDatabase.COLUMN_ID, id);
	    values.put(TimeDatabase.COLUMN_ACT, activity);
	    values.put(TimeDatabase.COLUMN_SEC, (long)calnow.get(Calendar.SECOND)+(long)calnow.get(Calendar.MINUTE)*60+(long)calnow.get(Calendar.HOUR)*3600);
	    values.put(TimeDatabase.COLUMN_DAY, (long)calnow.get(Calendar.DAY_OF_MONTH));
	    long insertId = database.insert(TimeDatabase.TABLE_NAME, null, values);
	  }
	  //TimeDatabase.TABLE_NAME, TimeDatabase.COLUMN_SEC, TimeDatabase.COLUMN_ACT + " = " + insertId, null, null, null, null);
	  
	  public int readEvent(String activity) {
		  	String[] Column = {TimeDatabase.COLUMN_SEC};//"(SELECT SUM("+ TimeDatabase.COLUMN_SEC +") FROM" + TimeDatabase.TABLE_NAME +" ) AS OrderTotal "};
		  	String[] Args = {activity};
		    Cursor cursor = database.query(TimeDatabase.TABLE_NAME, Column, TimeDatabase.COLUMN_ACT + " = ?", Args , null, null, null);
		    cursor.moveToFirst();
//		    int sum = cursor.getInt(1);
		    cursor.close();
		    return 888;
		  }
	  
	  
}
